/**
 * @class remoteAppUI
 * @description User Inteface class for the Attendance application
 * @author CLLM
 * @version 0.1
 */

// Initial class Called from main 

import java.awt.*;
import java.awt.event.*;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Vector;
import javax.swing.*;

public class remoteAppUI implements ActionListener, WindowListener, Serializable{

	//Variables
	Event load=null;

	//Shared
	Calendar curTime = Calendar.getInstance();	//For clock
	SimpleDateFormat dateFormat = new SimpleDateFormat("HH:mm:ss MM-dd-yyyy"); //For clock

	//Window Specific
	JPanel PrimaryPanel, mainMenuPanel, newEventPanel, loadEventPanel, attendanceIDPanel;
	JLabel lblDateTime;
	JFrame mainFrame = new JFrame("Welcome to AdCreate Society!");
	JFrame secondFrame = new JFrame("AdCreate");

	//Input
	JButton btnNew, btnLoad, btnExit, btnNCreate;

	//Images
	private Image imgMainBackgound ;//= Toolkit.getDefaultToolkit().createImage("img/RemoteApp_D1.jpg");
	Image imgLogo = Toolkit.getDefaultToolkit().createImage("img/Dlsu.png");
	Image imgNew = Toolkit.getDefaultToolkit().createImage("img/newEvent.png");
	Image imgLoad = Toolkit.getDefaultToolkit().createImage("img/load.png");
	Image imgExit = Toolkit.getDefaultToolkit().createImage("img/exit.png");
	ImageIcon logo, new1, load1, exit;
	

	public remoteAppUI()
	{
		Image newimg = imgNew.getScaledInstance(20, 20, java.awt.Image.SCALE_SMOOTH) ;  
		new1 = new ImageIcon( newimg );
		Image newimg2 = imgLoad.getScaledInstance(25, 25, java.awt.Image.SCALE_SMOOTH) ;  
		load1 = new ImageIcon( newimg2 );
		Image newimg3 = imgExit.getScaledInstance(23, 23, java.awt.Image.SCALE_SMOOTH) ;  
		exit = new ImageIcon( newimg3 );
		
		imgMainBackgound = new ImageIcon("img/RemoteApp_D1.gif").getImage();		
		
		System.out.println(dateFormat.format(curTime.getTime()));
		JFrame.setDefaultLookAndFeelDecorated(false);
		//ImagePanel primaryPanel = new ImagePanel(imgMainBackgound);
		//primaryPanel.setLayout(null);
		
		//mainFrame.getContentPane().add(primaryPanel);
		mainFrame.setIconImage(imgLogo);
		mainFrame.addWindowListener(this);
		mainFrame.setLayout(null);
		mainFrame.setSize(320,318);
		mainFrame.setResizable(false);
		mainFrame.setLocationRelativeTo(null);
		mainFrame.setVisible(true);
		mainFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		
		ImageIcon imageIconAdd = new ImageIcon("icon/add.gif");

		btnNew = new JButton ("New Event");
		btnLoad = new JButton ("Load");
		btnExit = new JButton ("Exit");
				
		mainMenuPanel = new ImagePanel(imgMainBackgound);
		mainMenuPanel.setLocation(0, 0);
		mainMenuPanel.setSize(320, 300);
		
		btnNew.setIcon(new1);
		btnLoad.setIcon(load1);
		btnExit.setIcon(exit);
		
		btnNew.setBackground(Color.getHSBColor(0.3f, 0.2f, 0.9f));
		btnLoad.setBackground(Color.getHSBColor(0.3f, 0.2f, 0.9f));
		btnExit.setBackground(Color.getHSBColor(0.1f, 0.2f, 0.9f));
			
		btnNew.setMnemonic('n');
		btnNew.addActionListener(this);
		btnNew.setBounds(120, 130, 150, 35);
		btnLoad.setMnemonic('l');
		btnLoad.addActionListener(this);
		btnLoad.setBounds(120, 180, 150, 35);
		btnExit.setMnemonic('x');
		btnExit.addActionListener(this);
		btnExit.setBounds(120, 230, 150, 35);
		mainMenuPanel.add(btnNew);
		mainMenuPanel.add(btnLoad);
		mainMenuPanel.add(btnExit);
		//primaryPanel.add(mainMenuPanel);
		mainFrame.getContentPane().add(mainMenuPanel);

	}

	//	public void newEventInitialize(){
	//		
	//		secondFrame.setLayout(null);
	//		secondFrame.setContentPane(secondaryPanel);
	//		secondFrame.setSize(300,250);
	//		secondFrame.setLocationRelativeTo(null);
	//		secondFrame.setVisible(true);
	//		secondFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
	//		
	//		newEventPanel =  new JPanel();
	//		newEventPanel.setLocation(20, 20);
	//		newEventPanel.setSize(300, 250);
	//		newEventPanel.setLayout(null);
	//		
	//		btnNCreate = new JButton ("Create");
	//		btnNCreate.setBounds(10, 10, 10, 10);
	//		newEventPanel.add(btnNCreate);
	//		
	//		//secondaryPanel.add(newEventPanel);
	//	}


	public void actionPerformed(ActionEvent arg0) {
		Object source = arg0.getSource();
		if (source == btnExit){
			windowClosing(null);
		}
		else if (source == btnLoad){
			//JOptionPane.showMessageDialog(null, "Under Construction", "Under Construction", JOptionPane.PLAIN_MESSAGE);
			FileDialog fd = new FileDialog(mainFrame, "Load Database From..", FileDialog.LOAD);
			fd.setVisible(true);
			try{
				if(fd.getFile().substring(fd.getFile().indexOf('.')).equals(".adcreate"))
				{
					System.out.print("Loading...\n");
					//FILE LOADER HERE
					//LOAD THE EVENT FILE AND SAVE IT TO "load"
					FileInputStream fileStreamInput=null;
					ObjectInputStream outputStreamInput=null;

					try{
						//Load from file A.ser
						fileStreamInput= new FileInputStream(fd.getDirectory() + fd.getFile());
						outputStreamInput = new ObjectInputStream(fileStreamInput);
						load = (Event) outputStreamInput.readObject();
						System.out.println("DEBUG:::load: " + load.StudentList.size());
					} catch(IOException e){
						e.printStackTrace();
					} catch (ClassNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					finally{
						try {
							outputStreamInput.close();
							fileStreamInput.close();
						}
						catch (IOException e1) {
							e1.printStackTrace();
						}
					}


					System.out.print("Load Successful. new event window loading...\n");
					mainFrame.setVisible(false);
					new NewEvent(mainFrame, load);
				}
				else
					System.out.print("ERROR: INVALID FILE FORMAT!\n");
			}
			catch(NullPointerException e){

			}
		}
		else if (source == btnNew){
			//newEventInitialize();
			System.out.print("new event window loading...\n");
			mainFrame.setVisible(false);
			new NewEvent(mainFrame);
			/*
			Vector<Course> tmp=new Vector<Course>();
			tmp.add(new Course("a"));
			tmp.get(0).Sections.add("b");
			new NewEvent(mainFrame, new Event(0,tmp,"abc"),true);
			 */
		}

	}

	public void windowActivated(WindowEvent e) {
	}

	public void windowClosed(WindowEvent e) {
	}

	public void windowClosing(WindowEvent e) {
		if(JOptionPane.showConfirmDialog(null, "Are you sure you want to exit?", "Are you sure?", JOptionPane.YES_NO_OPTION)==JOptionPane.YES_OPTION){
			System.exit(0);
		}
	}

	public void windowDeactivated(WindowEvent e) {
	}

	@Override
	public void windowDeiconified(WindowEvent e) {
	}

	public void windowIconified(WindowEvent e) {
	}

	public void windowOpened(WindowEvent e) {
	}

}


